<?php

include('../resource/db/db.php');
$pageTitle = "学习卡组";
$currentPage = "set";

if (!isset($_COOKIE['user_id'])) {
    header("Location: ../main/login.php");
    exit();
}

$user_id = $_COOKIE['user_id'];

$sql = "SELECT sets.set_id, sets.name, sets.info, sets.reward 
        FROM sets 
        WHERE sets.user_id='$user_id' AND sets.is_deleted=FALSE";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的卡组</title>
    <link rel="stylesheet" href="../resource/css/styles.css">
</head>

<body>
    <?php include('../resource/navbar.php'); ?>
    <div class="container">
        <!-- 接受set_id并展示对应的卡组 -->
        <?php
        if (isset($_GET['set_id'])) {
            $set_id = $_GET['set_id'];
            $set_sql = "SELECT * FROM sets WHERE set_id='$set_id' AND is_deleted=FALSE";
            $set_result = $conn->query($set_sql);
            $set = $set_result->fetch_assoc();

            if ($set) {
                if ($_SERVER['REQUEST_METHOD'] == 'POST') {
                    if (isset($_POST['update_set'])) {
                        $new_name = $_POST['set_name'];
                        $new_info = $_POST['set_info'];

                        $update_sql = "UPDATE sets SET name='$new_name', info='$new_info' WHERE set_id='$set_id' AND user_id='$user_id'";
                        if ($conn->query($update_sql)) {
                            echo "<div class='alert alert-success'>卡组信息已更新</div>";
                            $set['name'] = $new_name;
                            $set['info'] = $new_info;
                        } else {
                            echo "<div class='alert alert-danger'>更新失败：" . $conn->error . "</div>";
                        }
                    }
                }

                echo "<h2>" . htmlspecialchars($set['name']) . "</h2>";
                echo "<p>简介: " . htmlspecialchars($set['info']) . "</p>";
                echo "<p>热度: " . $set['reward'] . "</p>";
                echo "<a href='../main/user-index.php'>返回首页</a>";
                echo "<ul class='list-group'>";

                $cards_sql = "SELECT * FROM cards WHERE set_id='$set_id' AND is_deleted=FALSE";
                $cards_result = $conn->query($cards_sql);
                if ($cards_result->num_rows > 0) {
                    while ($card = $cards_result->fetch_assoc()) {
                        echo "<div class='front'>
                            <a href='../cards/detail.php?card_id=" . $card['card_id'] . "&set_id=" . $set_id . "' class='set-link'>
                                <li class='list-group-item'>" . $card['title'] . "
                            </a>
                                    <span style='float:right'>正面</span>
                                </li>
                        </div>";

                        echo "<div class='back' style='display:none;'>
                            <a href='../cards/detail.php?card_id=" . $card['card_id'] . "&set_id=" . $set_id . "' class='set-link'>
                                <li class='list-group-item'>" . $card['content'] . "
                            </a>
                                <span style='float:right'>反面</span>
                                </li>
                        </div>";
                    }
                } else {
                    echo "<li class='list-group-item'>卡组中没有卡片</li>";
                }
                echo "</ul>";

                echo "<h3>修改卡组信息</h3>";
                echo "<div class='container'>";
                echo "<form method='post' action=''>";
                echo "<div class='form-group'>";
                echo "<label for='set_name'>卡组名称</label>";
                echo "<input type='text' id='set_name' name='set_name' class='form-control' value='" . htmlspecialchars($set['name']) . "' required>";
                echo "</div>";
                echo "<div class='form-group'>";
                echo "<label for='set_info'>卡组简介</label>";
                echo "<textarea id='set_info' name='set_info' class='form-control' required>" . htmlspecialchars($set['info']) . "</textarea>";
                echo "</div>";
                echo "<button type='submit' name='update_set' class='btn btn-primary'>更新</button>";
                echo "</form>";
                echo "</div>";
            } else {
                echo "<div class='alert alert-danger'>没有找到相关卡组</div>";
                echo "<a href='../main/user-index.php'>返回首页</a>";
            }
        } else {
            echo "<h2>我的卡组</h2>";
            echo "<ul class='list-group'>";
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    echo "<a href='set.php?set_id=" . $row['set_id'] . "' class='set-link'>";
                    echo "<li class='list-group-item'>" . htmlspecialchars($row['name']) . "</li>";
                    echo "</a>";
                }
            } else {
                echo "<li class='list-group-item'>您还没有卡组</li>";
            }
            echo "</ul>";
        }
        ?>
    </div>
</body>
<script>
    // ul中所有卡片都可点击button切换另一面，点击正面显示反面，点击反面显示正面
    var front = document.querySelectorAll('.front');
    var back = document.querySelectorAll('.back');
    for (var i = 0; i < front.length; i++) {
        front[i].addEventListener('click', function() {
            this.style.display = 'none';
            this.nextElementSibling.style.display = 'block';
        });
    }

    for (var i = 0; i < back.length; i++) {
        back[i].addEventListener('click', function() {
            this.style.display = 'none';
            this.previousElementSibling.style.display = 'block';
        });
    }
</script>

</html>
